home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 008 / 68kgraph.arc / SQRT.ASM < prev   
Encoding:
Assembly Source File  |  1986-12-13  |  1.4 KB  |  39 lines

  1. ******************************************************************
  2. * COPYRIGHT (C) 1986 by Donald Krantz and James Stanley
  3. * - Note: This is a real, live, actual, registered copyright,
  4. *   and should be treated as such. This source code is from
  5. *   the book "68000 Assembly Language", Krantz and Stanley,
  6. *   Addison-Wesley Publishing Company, Reading, MA, 1986.
  7. *   Permission granted by the authors for non-commercial use
  8. *   in programs released to the public domain, as long as this
  9. *   copyright notice remains attached and visible.
  10. *
  11. *****************************************************************
  12. * SQRT - Integer floored square root
  13. *    Entry:     D0.L = number to take root of
  14. *    Exit:    D0.L = square root
  15.  
  16.     xdef    sqrt
  17.  
  18. sqrt:
  19.     movem.l    d1-d3,-(a7)
  20.     clr.l    d1        * D1 will be test case root
  21.     move.w    #$8000,d2    * D2 is succ. aprox. mask
  22. lp1:
  23.     or.w    d2,d1        * load in mask value
  24.     move.w    d1,d3        * d3 is a scratch register
  25.     mulu    d3,d3        * make a try
  26.     cmp.l    d3,d0        * take a look - how'd we do?
  27.     bhi    sk1        * try less than goal - keep bit
  28.     not.w    d2        * we need to mask bit out 
  29.     and.w    d2,d1        * take out trial bit
  30.     not.w    d2        * fix mask
  31. sk1:
  32.     lsr.w    #1,d2        * move mask down
  33.     bcc    lp1        * mask mot done - go around.
  34. sq_exit:
  35.     move.l    d1,d0        * transfer answer
  36.     movem.l    (a7)+,d1-d3    * restore registers
  37.     rts
  38.